package com.pxr.slt.client;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.DefaultedHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.pxr.tutorial.xmltest.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class NewTweet extends Activity
{
	EditText tweetText;
	String userName;

   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.newtweet);
      SharedPreferences androidRef = getSharedPreferences("username", MODE_PRIVATE);
      userName = androidRef.getString("username","");      
      
      String function = this.getIntent().getStringExtra("action");
      String user = this.getIntent().getStringExtra("user");
      tweetText =  (EditText) findViewById(R.id.tweetText);
      Bundle extras = getIntent().getExtras();
      if (extras != null) {
    	  if(extras.getString("action").equals("reply")){
    		  tweetText.setText(extras.getString("user"+" "));
    	  } else if(extras.getString("action").equals("retweet")){
    		  if(extras.getString("message").indexOf("@")>0){
    			  tweetText.setText(extras.getString("message"));
    		  } else {
    			  tweetText.setText(extras.getString("user")+": \""+extras.getString("message")+"\" ");
    		  }
    	  }
    	  
          //Toast.makeText(NewTweet.this, extras.getString("action") + extras.getString("user") , Toast.LENGTH_SHORT).show();
      }      
      tweetText.setSelection(tweetText.getText().toString().length());
      
    Button verzenden = (Button) findViewById(R.id.verzenden);
    verzenden.setOnClickListener(new OnClickListener(){ // Add OnClick listener for when the user clicks the button      
	    @Override
	    public void onClick(View v) {
	    	
	    	if(tweetText.getText().toString().length() == 0){
	    		Toast.makeText(NewTweet.this, "Uw tweet is leeg", Toast.LENGTH_SHORT).show();
	    	} else {
	    		HttpClient httpClient = new DefaultHttpClient();
	    		HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 2000);
	    		HttpConnectionParams.setSoTimeout(httpClient.getParams(), 2000);
	    		HttpPost httpPost = new HttpPost(Main.SERVER_ADDRESS + "message/addTweet");  
	    		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
	    		nameValuePairs.add(new BasicNameValuePair("submit", "1"));  
	    		nameValuePairs.add(new BasicNameValuePair("message", tweetText.getText().toString()));
	    		nameValuePairs.add(new BasicNameValuePair("user", userName)); 
	    		//
	    		try {
					httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
					HttpResponse response = httpClient.execute(httpPost);
					if(EntityUtils.toString(response.getEntity()).length()>0){
				    	Intent intent = new Intent(NewTweet.this, Main.class);
				    	startActivity(intent);
					} else {
						Toast.makeText(NewTweet.this, "Tweet plaatsen mislukt", Toast.LENGTH_SHORT).show();
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
	    		
	    		
	    	}
	    }
	}); 
    
    Button vorige = (Button) findViewById(R.id.vorige);
    vorige.setOnClickListener(new OnClickListener(){ // Add OnClick listener for when the user clicks the button      
	    @Override
	    public void onClick(View v) {
	    	Intent intent = new Intent(NewTweet.this, Main.class);
	    	startActivity(intent);
	    }
	});     
   }
   
}